var app = new Vue({
	el: '#app',
	data: {
		oldpassword: '',
		newpassword: '',
		newname: '',
		adminMsg: {
			isLogin: false,
			id: null,
			account: null,
			name: null,
			headimg: null
		},
		menu: 'all',
		userMsg: [],
		showUser: {}
	},
	computed: {
		showUserList: function(){
			var ret = [];
			if (this.menu == "all") {
				for (var i = 0; i< this.userMsg.length; i++) {
					if (this.userMsg[i].able == 1) {
						ret.push(this.userMsg[i]);
					}
				}
			}
			if (this.menu == 'delete') {
				for (var i = 0; i< this.userMsg.length; i++) {
					if (this.userMsg[i].able == 0) {
						ret.push(this.userMsg[i]);
					}
				}
			}
			return ret;
		}
	},
	methods: {
		changePassword: function(){
			var self = this;
			var id = self.adminMsg.id;
			var oldpassword = self.oldpassword;
			var newpassword = self.newpassword;
			var data = {
				"id": id,
				"oldpassword": oldpassword,
				"newpassword": newpassword
			}
			$.post("/api/admin/changePassword", data, function(data,status){
				if (data.success == "ok") {
					$("#passwordModal").modal("hide");
					swal("修改成功","","success");
				} else {
					swal("修改失败",data.msg, "error");
				}
			})
		},
		changeName: function(){
			var self = this;
			var id = self.adminMsg.id;
			var name = self.newname;
			var data = {
				"id": id,
				"name": name
			}
			$.post("/api/admin/setAdmin", data, function(data,status){
				if (data.success == "ok") {
					$("#nameModal").modal("hide");
					self.getLoginStatus();
					swal("修改成功","","success");
				} else {
					swal("修改失败",data.msg, "error");
				}
			})
		},
		logout: function(){
			var self = this;
			swal({ 
				title: "确定退出吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post("/api/admin/logout",{},function(data,status){
					if (data.success = "ok") {
						self.getLoginStatus();
					}
				});
			});
		},
		getLoginStatus: function(){
			var self = this;
			$.get('/api/admin/isLogin',function(data,status){
				if (data.success == "ok") {
					self.adminMsg.isLogin = true;
					self.adminMsg.name = data.result.name;
					self.adminMsg.id = data.result.id;
					self.adminMsg.account = data.result.account;
					self.adminMsg.headimg = data.result.headimg;
				} else {
					self.adminMsg.isLogin = false;
					window.location.href = "/admin/login.html"
				}
			})
		},
		changeMenu: function(param){
			this.menu = param;
		},
		getUser: function(){
			var self = this;
			$.post('/api/admin/getUser',{}, function(data,status){
				if (data.success == "ok") {
					self.userMsg = data.result;
				} else {
					swal("网络错误","","error");
				}
			})
		},
		showUserMsg: function(i){
			this.showUser = this.showUserList[i];
			$("#userMsgModal").modal("show");
			console.log(i)
		},
		deleteUser: function(id){
			var self = this;
			var data = {"able": 0,"id":id};
			swal({ 
				title: "确定禁用吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post('/api/admin/setUser',data,function(data,status){
					if (data.success == "ok") {
						swal("设置禁用成功","","success");
						self.getUser();
					} else {
						swal("发生错误","可能网络出现了问题...","error");
					}
				})
			});
		},
		recoverUser: function(id){
			var self = this;
			var data = {"able": 1,"id":id};
			swal({ 
				title: "确定恢复吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post('/api/admin/setUser',data,function(data,status){
					if (data.success == "ok") {
						swal("恢复成功","","success");
						self.getUser();
					} else {
						swal("发生错误","可能网络出现了问题...","error");
					}
				})
			});
		}
	},
	mounted: function(){
		this.getLoginStatus();
		this.getUser();
	}
})
